package indi.gtl.superbrain.activitys;

import indi.gtl.superbrain.R;
import indi.gtl.superbrain.tools.Databasehelper;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActiviry extends Activity {
	EditText register_username_text, register_password_text;
	Button register_register_but, register_esc_but, register_return_btn;
	Databasehelper databasehelper;
	SQLiteDatabase database;
	String accountnumber, cipher;
	ActionBar actionBar;
	Intent intent;
	Cursor cursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_layout);
		register_username_text = (EditText) findViewById(R.id.register_username_text);
		register_password_text = (EditText) findViewById(R.id.register_password_text);
		register_register_but = (Button) findViewById(R.id.register_register_but);
		register_esc_but = (Button) findViewById(R.id.register_esc_but);
		register_return_btn = (Button) findViewById(R.id.register_return_btn);
		databasehelper = new Databasehelper(this);
		database = databasehelper.getWritableDatabase();
		actionBar = getActionBar();
		actionBar.hide();
		register_register_but.setOnClickListener(clickListener);
		register_esc_but.setOnClickListener(clickListener);
		register_return_btn.setOnClickListener(clickListener);
	}

	/*
	 * ���ü�����1.����ע����˺�����ת���ַ�������accountnumber��cipher.
	 * 2.��ȡ���ݿ�����ı�����˵�accountnumber��cipher
	 */
	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.register_register_but:
				accountnumber = register_username_text.getText().toString();
				cipher = register_password_text.getText().toString();
				
				if (accountnumber.equals("")) {
					Toast.makeText(RegisterActiviry.this, "账号不能为空",
							Toast.LENGTH_SHORT).show();
				} else {
					if (cipher.equals("")) {
						Toast.makeText(RegisterActiviry.this, "密码不能为空",
								Toast.LENGTH_SHORT).show();

					} else {
						cursor = Databasehelper.JudgeAccountnumber(database,
								accountnumber);
						if (cursor.moveToNext()) {
							Toast.makeText(RegisterActiviry.this, " 账号以存在",
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(RegisterActiviry.this, " 注册成功",
									Toast.LENGTH_SHORT).show();
							Databasehelper.Data(database, accountnumber,
									cipher, RegisterActiviry.this);
							finish();
						}
					}
				}

				break;

			case R.id.register_esc_but:
				finish();
				break;
			case R.id.register_return_btn:
				finish();
				break;
			}

		}
	};

}